#import "ScalpSSHPublishOperation.h"
#import "util.h"
#import "SSHConnection.h"

@implementation ScalpSSHPublishOperation

+(BOOL)canHandleURL:(NSURL *)url
{
    // we handle URLs beginning with ssh:// and sftp://
    NSString *scheme = [url scheme];
    return ([scheme isEqualToString:@"ssh"] || [scheme isEqualToString:@"sftp"]);
}

+(NSError *)scalpErrorWithError:(NSError *)err
{
    return scalpPublishError([err code], [[err userInfo] valueForKey:NSLocalizedRecoverySuggestionErrorKey]);
}

- (id)initWithPublisher:(CALURLPublisher *)publisher withURL:(NSURL *)url
{
    self = [super initWithPublisher:publisher withURL:url];
    _url = url; // for some reason, the super init method isn't setting _url
    return self;
}

-(void)publishData:(NSData *)data automatic:(BOOL)automatic
{
    NSError *err = [SSHConnection scpData:data to:_url];
    if (err)
        [self setError:[ScalpSSHPublishOperation scalpErrorWithError:err]];
}

- (void)unpublishDataWithAutomatic:(BOOL)automatic
{
	NSError *err = [SSHConnection deleteFileAtURL:_url];
    if (err)
        [self setError:[ScalpSSHPublishOperation scalpErrorWithError:err]];
}

@end
